﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class FactoryGoodsCHN2ENGTab : DockContent
    {
        private DGBY.BLL.FactoryGoodsTab m_bllFactoryGoodsTab = new DGBY.BLL.FactoryGoodsTab();
        private DGBY.BLL.FactoryGoodsCHN2ENGTab m_bllFactoryGoodsCHN2ENGTab = new DGBY.BLL.FactoryGoodsCHN2ENGTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectPortID = 0;

        public FactoryGoodsCHN2ENGTab()
        {
            InitializeComponent();
        }

        private void FactoryGoodsCHN2ENGTab_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllFactoryGoodsTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxGoodsChnName.DataSource = ds.Tables[0];
                this.comboBoxGoodsChnName.DisplayMember = "货品名称";
                this.comboBoxGoodsChnName.ValueMember = "FactoryGoodsTabID";
                this.comboBoxGoodsChnName.SelectedIndex = 0;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDelPort.Visible = true;
            }
        }
        private void ShowAllRecordInDataGridView(string strWhere)
        {
            dataGridViewPortList.DataSource = null;
            m_ds = m_bllFactoryGoodsCHN2ENGTab.GetList(strWhere);
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewPortList.DataSource = m_ds.Tables[0];
                dataGridViewPortList.Columns[0].Visible = false;
                dataGridViewPortList.Columns[1].Visible = false;
            }
        }

        private void buttonShowAllRecordPort_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView("");
        }

        private void buttonAddPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FactoryGoodsCHN2ENGTab modelFactoryGoodsCHN2ENGTab = new DGBY.Model.FactoryGoodsCHN2ENGTab();
                modelFactoryGoodsCHN2ENGTab.FactoryGoodsEngName = this.textBoxGoodsEngName.Text;
                modelFactoryGoodsCHN2ENGTab.ReservField1 = "";

                if (this.comboBoxGoodsChnName.SelectedValue != null)
                    modelFactoryGoodsCHN2ENGTab.FactoryGoodsTabID = Convert.ToInt32(this.comboBoxGoodsChnName.SelectedValue.ToString(), 10);

                m_bllFactoryGoodsCHN2ENGTab.Add(modelFactoryGoodsCHN2ENGTab);


                m_nSelectPortID = m_bllFactoryGoodsCHN2ENGTab.GetMaxId() - 1;
                ShowAllRecordInDataGridView("");
                //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonModifyPort_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.FactoryGoodsCHN2ENGTab modelFactoryGoodsCHN2ENGTab = m_bllFactoryGoodsCHN2ENGTab.GetModel(m_nSelectPortID);
                if (modelFactoryGoodsCHN2ENGTab == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                modelFactoryGoodsCHN2ENGTab.FactoryGoodsEngName = this.textBoxGoodsEngName.Text;
                modelFactoryGoodsCHN2ENGTab.ReservField1 = "";

                if (this.comboBoxGoodsChnName.SelectedValue != null)
                    modelFactoryGoodsCHN2ENGTab.FactoryGoodsTabID = Convert.ToInt32(this.comboBoxGoodsChnName.SelectedValue.ToString(), 10);

                m_bllFactoryGoodsCHN2ENGTab.Update(modelFactoryGoodsCHN2ENGTab);

                ShowAllRecordInDataGridView("");
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        private void cellclick()
        {
            DGBY.Model.FactoryGoodsCHN2ENGTab modelFactoryGoodsCHN2ENGTab = m_bllFactoryGoodsCHN2ENGTab.GetModel(m_nSelectPortID);
            if (modelFactoryGoodsCHN2ENGTab != null)
            {
                this.textBoxGoodsEngName.Text = modelFactoryGoodsCHN2ENGTab.FactoryGoodsEngName;

                if (modelFactoryGoodsCHN2ENGTab.FactoryGoodsTabID != null)
                    this.comboBoxGoodsChnName.SelectedValue = modelFactoryGoodsCHN2ENGTab.FactoryGoodsTabID;
            }
        }

        private void buttonDelPort_Click(object sender, EventArgs e)
        {
            DGBY.Model.FactoryGoodsCHN2ENGTab modelFactoryGoodsCHN2ENGTab = m_bllFactoryGoodsCHN2ENGTab.GetModel(m_nSelectPortID);

            if (modelFactoryGoodsCHN2ENGTab == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllFactoryGoodsCHN2ENGTab.Delete(m_nSelectPortID);
            ShowAllRecordInDataGridView("");
            this.textBoxGoodsEngName.Text = "";

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dataGridViewPortList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strPortID = this.dataGridViewPortList.Rows[this.dataGridViewPortList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectPortID = Convert.ToInt32(strPortID, 10);
            cellclick();
        }

        private void dataGridViewPortList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewPortList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void comboBoxGoodsChnName_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string str = this.comboBoxGoodsChnName.SelectedValue.ToString();
                this.dataGridViewPortList.DataSource = null;
                //int nID = Convert.ToInt32(str, 10);
                ShowAllRecordInDataGridView("FactoryGoodsCHN2ENGTab.FactoryGoodsTabID = '" + str + "'");
            }
            catch { }
        }
    }
}
